﻿<?php
require('configs/include.php');

class c_ingresar_programa_a_modulo extends super_controller {

    public function ingresar() {
        if (is_empty($this->post->programas_a_ingresar) || is_empty($this->post->select_modulo)) {
            throw_exception("Debe seleccionar Programas, Módulo y Kernel antes de continuar");
        } else {
            $array_id_programa = $this->post->programas_a_ingresar;
            for ($i = 0; $i < count($array_id_programa); $i++) {
                $programa = new programa();
                $programa->set('id', $array_id_programa[$i]);
                $programa->set('modulo', $this->post->select_modulo);
                $this->orm->connect();
                $this->orm->update_data("by_id", $programa);
                $this->orm->close();

                $this->type_warning = "success";
                $this->msg_warning = "El programa se ingresó correctamente";
                $this->img_warning = "correct.png";
                $this->engine->assign('img_warning', $this->img_warning);
                $this->engine->assign('msg_warning', $this->msg_warning);
                $this->temp_aux = 'message.tpl';
            }
        }
    }

    public function cargar_programas_y_kernel() {
        $options['programa']['lvl2'] = "by_estado";
        $options['kernel']['lvl2'] = "by_state";
        $this->orm->connect();
        $this->orm->read_data(array("programa", "kernel"), $options);
        $programas = $this->orm->get_objects("programa");
        $kernels = $this->orm->get_objects("kernel");
        $this->orm->close();
        $this->engine->assign('programas', $programas);
        $this->engine->assign('kernels', $kernels);
//        $this->display();

    }

    public function display() {
        $this->cargar_programas_y_kernel();
        $varAux = $this->gvar['n_index'] . " -> " . $this->gvar['n_program'];
        $this->engine->assign('ubicationPath', $varAux);
        $this->engine->display('header.tpl');
        $this->engine->display('ingresar_programa_a_modulo.tpl');
        $this->engine->display($this->temp_aux);
        $this->engine->display('footer.tpl');
    }

    public function run() {
        if ($_SESSION['usuario']['tipo'] == "lider") {
            try {
                if (isset($this->get->option)) {
                    $this->{$this->get->option}();
                }
            } catch (Exception $e) {
                $this->error = 1;
                $this->msg_warning = $e->getMessage();
                $this->img_warning = "incorrect.png";
                $this->engine->assign('type_warning', $this->type_warning);
                $this->engine->assign('msg_warning', $this->msg_warning);
                $this->engine->assign('img_warning', $this->img_warning);
                $this->temp_aux = 'message.tpl';
            }
            $this->display();
        } else {
            $this->engine->display('header.tpl');
            $this->engine->display('sinPermisos.tpl');
            $this->engine->display('footer.tpl');
        }
    }

}

$call = new c_ingresar_programa_a_modulo();
$call->run();
?>